package com.asiainfo.bms.service.impl;

import com.asiainfo.bms.mapper.EmployeeMapper;
import com.asiainfo.bms.model.Employee;
import com.asiainfo.bms.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author liu.hx
 * @since 2021-07-13
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    /**
     * 分页查询
     *
     * @param page     当前页码
     * @param limit    每页记录数
     * @param employee 员工
     * @return IPage类实例
     */
    @Override
    public IPage<Employee> pageSelect(Long page, Long limit, Employee employee) {
        // 创建条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = Wrappers.lambdaQuery();
        if (employee != null) {
            String no = employee.getNo();
            String name = employee.getName();
            Integer borrowedNum = employee.getBorrowedNum();
            // 设置查询条件
            if (!StringUtils.isEmpty(no)) {
                queryWrapper.eq(Employee::getNo, no);
            }
            if (!StringUtils.isEmpty(name)) {
                queryWrapper.like(Employee::getName, name);
            }
            if (borrowedNum != null) {
                queryWrapper.eq(Employee::getBorrowedNum, borrowedNum);
            }
        }
        return baseMapper.selectPage(new Page<>(page, limit), queryWrapper);
    }

    /**
     * 根据工号获取一个员工(判断员工是否存在)
     *
     * @param no 员工号
     * @return employee 员工
     */
    @Override
    public Employee getByNo(String no) {
        LambdaQueryWrapper<Employee> queryWrapper = Wrappers.lambdaQuery();
        if (no != null) {
            queryWrapper.eq(Employee::getNo, no);
        }
        return baseMapper.selectOne(queryWrapper);
    }
}
